package com.automation.selenium;

import java.io.File;
import java.io.FileInputStream;
import java.util.Locale;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

public class ExecuteTestScripts extends BaseClass {

  TestSummaryReport tsrObj = new TestSummaryReport();
  DateTimeFunctions dtfObj = new DateTimeFunctions();
  ObjectRepository orObj = new ObjectRepository();
  ExecuteKeywordScript eksObj = new ExecuteKeywordScript();



  public void FindExecTestscript(String TestSuite, String TestScript, String ObjectRepository)
      throws Exception {
    // Place holder for summary report header...
    tsrObj.createSummaryRptHeader();
    log("info", "Finding TestScripts....");
    FileInputStream fs = null;
    WorkbookSettings ws = null;
    fs = new FileInputStream(new File(TestSuite));
    ws = new WorkbookSettings();
    ws.setLocale(new Locale("en", "EN"));
    Workbook TSworkbook = Workbook.getWorkbook(fs, ws);
    Sheet TSsheet = TSworkbook.getSheet(0);
    TSsheet.getRows();
    for (int i = 0; i < TSsheet.getRows(); i++) {
      try {
        String TSvalidate = "r";
        if (((TSsheet.getCell(0, i).getContents()).equalsIgnoreCase(TSvalidate) == true)) {
          scriptNum = i;
          // String TCStatus = "Pass";
          String ScriptName = TSsheet.getCell(1, i).getContents();
          String ScriptORName = TSsheet.getCell(2, i).getContents();
          log("info", "Executing TestScript{" + ScriptName + "} using OR{" + ScriptORName + "}");
          stepNo = 0;
          testStatus = "PASS";

          String scriptORExcelPath = ObjectRepository + ScriptORName;
          orObj.generateScriptORMAP(scriptORExcelPath);
          eksObj.ExecKeywordScript(ScriptName, TestScript, ObjectRepository, ScriptORName);

          // Place holder to update the summary report after each test script completion-Yuva.
          if (testStatus.equalsIgnoreCase("PASS") == true) {
            log("info",
                "Updating summary report [after ExecKeywordScript completion] with test status as: "
                    + testStatus);
            tsrObj.updateSummaryReport(ScriptName, "executed");
          }
          if (testStatus.equalsIgnoreCase("FAIL") == true) {
            log("info",
                "Updating summary report [after ExecKeywordScript completion] with test status as: "
                    + testStatus);
            tsrObj.updateSummaryReport(ScriptName, "failed");
          }

        }
        /*
         * else { System.out.println(TSvalidate); }
         */
      } catch (Exception exTSuite) {
        log("error", "Exception in Test Suite FOR loop-" + exTSuite.getMessage());
      }
    }
    // Close the summary report bufferwriter here...Yuva
    executionEndTime = dtfObj.getDateTimeWithZone();
    totalExecutionTime = dtfObj.getElapsedTime(executionStartTime, executionEndTime);
    tsrObj.createSummaryRptFooter();
    bwSummaryRpt.close();
  }

}
